<template>
<div class="user">
    <div class="common-level-rail">
      <el-button
        size="small"
        type="primary"
        icon="Plus"
        @click="addClick"
        v-auth="'/auth/role/add'"
        >{{$t('auth.add_role')}}</el-button
      >
    </div>

    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table
          size="small"
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="roleId" :label="$t('auth.role_id')"></el-table-column>
          <el-table-column prop="roleName" :label="$t('auth.role_name')"></el-table-column>
          <el-table-column prop="sort" :label="$t('auth.sort')"></el-table-column>
          <el-table-column prop="createTime" :label="$t('auth.created_time')"></el-table-column>
          <el-table-column fixed="right" :label="$t('auth.actions')" width="90">
            <template #default="scope">
              <el-button
                @click="editClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/auth/role/edit'"
                >{{$t('auth.edit')}}</el-button
              >
              <el-button
                @click="deleteClick(scope.row)"
                type="text"
                size="small"
                v-auth="'/auth/role/delete'"
                >{{$t('auth.delete')}}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import AuthApi from "@/api/auth.js";
export default {
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: []
    };
  },
  created() {
    /*获取列表*/
    this.getTableList();
  },
  methods: {
    /*获取列表*/
    getTableList() {
      let self = this;
      self.loading = true;
      AuthApi.roleList({}, true).then(res => {
        self.loading = false;
        self.tableData = res.data;
      }).catch(error => {
        self.loading = false;
      });
    },
    /*打开添加*/
    addClick() {
      this.$router.push("/auth/role/add");
    },
    /*打开编辑*/
    editClick(row) {
      let self = this;
      this.$router.push({
        path: "/auth/role/edit",
        query: {
          roleId: row.roleId
        }
      });
    },
    /*刷新心也*/
    refresh() {
      this.reload();
      // 直接使用reload方法
    },
    /*删除*/
    deleteClick(row) {
      let self = this;
      ElMessageBox.confirm($t("auth.confirm_delete_prompt"), $t("auth.popup_title"), {
        confirmButtonText: $t("auth.confirm"),
        cancelButtonText: $t("auth.cancel"),
        type: "warning"
      }).then(() => {
        self.loading = true;
        AuthApi.roleDelete({
          roleId: row.roleId
        }, true).then(data => {
          self.loading = false;
          if (data.code == 1) {
            ElMessage({
              message: $t("auth.delete_role_success"),
              type: "success"
            });
            self.getTableList();
          } else {
            self.loading = false;
          }
        }).catch(error => {
          self.loading = false;
        });
      }).catch(() => {});
    }
  }
};
</script>

